package com.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * 存在重复元素 II
 */
public class ContainsDuplicateIi {

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])){
                if (i-map.get(nums[i])<=k){
                    return true;
                }
            }
            map.put(nums[i],i);
        }
        return false;
    }

    public static void main(String[] args) {
        int [] nums=new int[]{1,2,3,1};
        boolean result = containsNearbyDuplicate(nums, 3);
        System.out.println(result);
    }
}
